API Manager API

(4 reviews)

Traffic Management

Using public API Manager REST API

1- Create the API instance. Endpoint: POST https://anypoint.mulesoft.com/apimanager/api/v1/organizations/{organizationId}/environments/{environmentId}/apis.

Set endpoint.uri to null and omit endpoint.tlsContexts.outbound in body.

2- Create Upstreams. Endpoint: POST https://anypoint.mulesoft.com/apimanager/api/v1/organizations/{organizationId}/environments/{environmentId}/apis/{environmentApiId}/upstreams.

Body example:

{
  "label": "optional",
  "uri": "http://upstream1.com",
  "tlsContext": {
    "secretGroupId": "secret-group-id",
    "tlsContextId": "tls-context-id"
  }
}

3- Add routing configuration to API instance: Endpoint: PATCH https://anypoint.mulesoft.com/apimanager/api/v1/organizations/{organizationId}/environments/{environmentId}/apis/{environmentApiId}.

Body Example:

{
  "routing": [
    {
      "label": "if path matches",
      "rules": {
        "path": "/v1/(/.*)"
      },
      "upstreams": [
        {"id": "A", "weight": 100}
      ]
    },
    {
      "label": "if path does not match",
      "upstreams": [
        {"id": "B", "weight": 100}
      ]
    }
  ]
}

routing is an array of routes. Each route has rules (optional) and upstreams. Given a request, routes are analyzed in order. If the request matches all the rules (or the route doesn't have rules), it's routed to one of the route upstreams, using its weight as a probability. id’s can be retrieved from upstream creation response.

In the given example, a request with a path matching expression /v1/(/.*) will be redirected to upstream A. The rest of the requests will be redirected to upstream B.

More body examples:
{
    "routing": [
    {
      "upstreams": [
        {"id": "A", "weight": 10},
        {"id": "B", "weight": 90}
      ]
    }
  ]
}

In this case, 10% of the upstreams will be routed to upstream A\` and 90% will be routed to upstreamB\.

{
    "routing": [
    {
      "rules": {
        "headers": {
          "name": "test"
        }
      },
      "upstreams": [
        {"id": "A", "weight": 50},
        {"id": "B", "weight": 50}
      ]
    },
    {
      "upstreams": [
        {"id": "B", "weight": 100}
      ]
    }
  ]
}

In this example, requests with a header name with value test will be routed to upstreams A or B with 50% of probability each one. The rest of the request will be routed to upstream B.

Accepted rules:

  • path (regular expression)

  • host (regular expression)

  • methods (regular expression)

  • headers (object with regular expressions as key or value)

Check Flex Gateway documentation for more information.

Adding routing configuration to existing API instances

When an API instance is created adding “endpoint.uri” or “endpoint.tlsContexts.outbound”, an upstream is automatically created for it. Its id can be retrieved with the following request: GET https://anypoint.mulesoft.com/apimanager/api/v1/organizations/{organizationId}/environments/{environmentId}/apis/{environmentApiId}/upstreams. This upstream can be used in routing configuration along with other upstreams added to the instance as it is explained in the previous section.

Creating API Instance using API Manager XAPI

API Manager XAPI allows API instance creation with multiple upstreams and routing configuration in a simple request.

Endpoint: POST https://anypoint.mulesoft.com/apimanager/xapi/v1/organizations/{organizationId}/environments/{environmentId}/apis

Body example:

{
  ...
  "routing": [
    {
      "rules": {
        "methods": "GET"
      },
      "upstreams": [
        {"uri": "http://test1.com", "weight": 50},
        {"uri": "http://test2.com", "label": "test2", "weight": 50}
      ]
    },
    {
      "upstreams": [
        {"uri": "http://test1.com", "weight": 100}
      ]
    }
  ]
}

Instead of adding upstream id’s to routes, uri, label and tlsContexts are added. An upstream will be created for each combination. In this case, an upstream with uri http://test1.com and one with uri http://test2.com and label test2 will be created. GET requests will be distributed between both upstreams. The rest of the requests will be routed to the first one. endpoint.uri field must be omitted.

Updating API Instance using API Manager XAPI

API instance upstreams and routing can be updated in a single request using API Manager XAPI.

Endpoint: PATCH https://anypoint.mulesoft.com/apimanager/xapi/v1/organizations/{organizationId}/environments/{environmentId}/apis/{environmentApiId}

Body example:

{
  "routing": [
    {
      "upstreams": [
        {"id": "random-id", "weight": 10},
        {"id": "real-uuid", "weight": 90}
      ]
    }
  ],
  "upstreams": [
    {"id": "real-uuid", "uri": "http://test1.com"},
    {"id": "random-id", "uri": "http://test2.com"}
  ]
}

upstreams field can be added to the request. For each upstream, if the id corresponds to an upstream that exists for the API instance, it will be updated, otherwise a new upstream will be created. Current upstreams with ids not listed here will be deleted.

If routing is specified in the same request, referenced ids should be listed in the upstreams array. Once the new upstreams are created, these temporal ids will be replaced with the actual ones.


Reviews